<template>
    <div class="components-container">
        <sweet-modal ref="dialog" class="dialog">
        <h3>{{title}}</h3>
        <div style=" height:350px; margin:30px 50px">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">物品名称</label>
                    <div class="layui-input-inline">
                        <el-input v-model="chaData.DPGNAME" class="formWide" placeholder="请输入项目名称" :readonly="isreadonly"></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">输入码</label>
                    <div class="layui-input-inline">
                        <el-input v-model="chaData.DPGCODE" class="formWide" placeholder="请输入项目代码" :readonly="isreadonly"></el-input>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">物品类型</label>
                    <div class="layui-input-inline">
                        <!-- 新增中的物品类型 -->
                        <el-select v-model="chaData.DPGTYPE" placeholder="请选择项目类别">
                            <el-option v-for="item in chargeProjectTypes" :key="item.CodeValue" :label="item.CodeName" :value="item.CodeValue" :disabled="isreadonly"/>
                        </el-select>
                        <!-- 修改中的物品类型 -->
                        <!-- <el-input v-model="chaData.DPGTYPEDESC" class="formWide" placeholder="请选择项目类别" :readonly="isreadonly" v-if="this.isreadonly"></el-input> -->
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">所属厂家</label>
                    <div class="layui-input-inline">
                         <!-- 新增中的物品类型 -->
                        <el-select v-model="chaData.DPGVDRID" placeholder="请选择所属厂家" v-if="!this.isreadonly">
                            <el-option v-for="item in Manufactor" :key="item.VDRID" :label="item.VDRNAME" :value="item.VDRID" :readonly="isreadonly"/>
                        </el-select>
                        <!-- 详情中的所属厂家 -->
                        <el-input v-model="chaData.VDRNAME" class="formWide" placeholder="请输入所属厂家" :readonly="isreadonly"  v-if="this.isreadonly"></el-input>
                    </div>
                </div>
            </div>
            <div class="layui-form-item" v-if="isshowmoney">
                <div class="layui-inline">
                    <label class="layui-form-label">成本单价</label>
                    <div class="layui-input-inline">
                        <el-input v-model="chaData.DPGCOST" class="formWide" placeholder="请输入成本金额" :readonly="isreadonly"></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">销售单价</label>
                    <div class="layui-input-inline">
                        <el-input v-model="chaData.DPGSALE" class="formWide" placeholder="请输入销售金额" :readonly="isreadonly"></el-input>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">单位</label>
                    <div class="layui-input-inline">
                        <el-select v-model="chaData.DPGUNIT" placeholder="请选择计量单位">
                            <el-option v-for="item in chaUintsTypes" :key="item.value" :label="item.label" :value="item.value" :disabled="isreadonly" />
                        </el-select>
                    </div>
                    <!-- <div class="layui-input-inline">
                        <el-input v-model="chaData.DPGUNIT" class="formWide" placeholder="请输入单位" :readonly="isreadonly"></el-input>
                    </div> -->
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">预警数量</label>
                    <!-- 新增和修改中的预警数量 -->
                    <div class="layui-input-inline" v-if="this.isinfo">
                        <el-input-number v-model="chaData.DPGWARNNUM" :min="-99999" :max="99999" class="formWide" placeholder="请输入单价"></el-input-number>
                    </div>
                    <!-- 详情中的预警数量 -->
                    <div class="layui-input-inline" v-if="!this.isinfo">
                        <el-input v-model="chaData.DPGWARNNUM" class="formWide" placeholder="请输入所属厂家" :readonly="isreadonly"></el-input>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-inline">
                        <el-input v-model="chaData.DPGNUM" class="formWide" placeholder="排序" :readonly="!isinfo"></el-input>
                    </div>
                </div>
                <div class="layui-inline"  v-if="this.isshowtiaoma">
                    <label class="layui-form-label">条形码</label>
                    <!-- 详情中的预警数量 -->
                    <div class="layui-input-inline">
                        <el-input v-model="chaData.DPGBAINUM" class="formWide" placeholder="请输入条形码" :readonly="isreadonly"></el-input>
                    </div>
                </div>
                <!-- <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <el-input v-model="chaData.DPGSTATUS" class="formWide" placeholder="状态" :readonly="isreadonly"></el-input>
                    </div>
                </div> -->
            </div>
        </div>
        <div class="layui-form-item btnCenter">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="submit" :loading="this.$store.state.isloading" v-if="!(this.title == '物品类别维护详情') " >提交</el-button>
        </div>
        </sweet-modal>
    </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";
</style>
<script>
import sweetModal from "@/components/sweetModal";
import apiBase from "@/api/apiBase.js";
import {
    formatDate,
    timeStringToDate
  } from "@/utils/date.js";
  const ADD = "新增";
  const MODIFYPROJECT = "修改";
  const DELETE = "删除";
  const INFO = "详情";

export default {
    name: "warehousetypemain",
    data(){
        return{
            isshowtiaoma:false, //条形码显隐
            isreadonly: false, //是否只读
            isinfo: true,//详情页面全部不能修改
            title: null, //附件表名
            chaData: {
                DPGNAME: null,  //物品名称
                DPGCODE: null,  //输入码
                DPGTYPE: null,  //物品类型
                DPGUNIT: null,  //单位
                DPGVDRID: null, //所属厂家
                DPGWARNNUM: null, //预警数量
                DPGBAINUM: null, //条形码
                DPGNUM: null, //排序
                DPGSTATUS: null, //状态
                DPGCOST: null, //成本金额
                DPGSALE: null, //销售金额
            },
            chargeProjectTypes: [], //物品类型集合
            Manufactor: [],//所属厂家集合
            isshowmoney: null,//开化个性化是否显示成本金额和销售金额
            chaUintsTypes: [],//计量单位
            IsDPGPushCHA: null, //义乌个性化 区分商品和服务
        }
    },
    methods: {
        //功能操作
        open(order, id) {
            this.order = order;
            this.dataRepeat();
            this.initSelectData();
            switch (this.order) {
            case ADD:
                this.title = "物品类别维护新增";
                this.isreadonly = false;
                this.isinfo = true;
                this.isshowtiaoma = false;
                this.$refs.dialog.open();
                break;
            case MODIFYPROJECT:
                this.DetailDpg(id);
                this.title = "物品类别维护修改";
                this.isreadonly = false;
                this.isshowtiaoma = true;
                this.isinfo = true;
                this.$refs.dialog.open();
                break;
            case DELETE:
                // this.dataDelete(id);
                break;
            case INFO:
                this.DetailDpg(id);
                this.title = "物品类别维护详情";
                this.isreadonly = true;
                this.isshowtiaoma = true;
                this.isinfo = false;
                this.$refs.dialog.open();
                break;
            default:
            }
        },
        //物品类别新增
        addCreateDpg(){
            let data = JSON.parse(JSON.stringify(this.chaData))
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Depot.CreateDpg,
                res => {
                    this.$message({
                        type: "success",
                        message: "新增成功!"
                    });
                    this.$refs.dialog.close();
                    this.refreshData();
                }
            )
        },
        // 物品类别修改
        ModifyDpg(){
            let data = JSON.parse(JSON.stringify(this.chaData))
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Depot.ModifyDpg,
                res => {
                     this.$message({
                        type: "success",
                        message: "修改成功!"
                    });
                    this.$refs.dialog.close();
                    this.refreshData();
                }
            )
        },
        //物品类别详情
        DetailDpg(id) {
            let data = {
                DPGID: id
            };
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Depot.DetailDpg,
                res => {
                    this.chaData = res.data.content;
                }
            )
        },
        //初始化下拉框
        initSelectData(){
            this.QueryVdr();
            this.chargeProjectTypes = [];
            if(this.IsDPGPushCHA){
                this.GetDpgTypeYw();
            }else{
                this.GetDpgType();
            }
            this.getUint();
        },
        //获取所属厂家
        QueryVdr(){
            apiBase.DoService(
                {},
                apiBase.getApiAdress().Depot.QueryVdrByZC,
                res => {
                    this.Manufactor = res.data.content.OutData;
                }
            )
        },
        //获取物品类型
        GetDpgType(){
            apiBase.DoService(
                {},
                apiBase.getApiAdress().Code.GetDpgType,
                res => {
                    this.chargeProjectTypes = res.data.content;
                }
            )
        },
        //获取物品类型（义乌）
        GetDpgTypeYw(){
            apiBase.DoService(
                {},
                apiBase.getApiAdress().ChargeProject.QueryType, 
                res => {
                    let datas = res.data.content;
                    datas.forEach(item => {
                        if(item.CYPTYPE === "A"){
                            this.chargeProjectTypes.push(item);
                        }
                    });
                    this.chargeProjectTypes = this.chargeProjectTypes.map(item => ({
                        CodeName: item.CYPNAME,
                        CodeValue: item.CYPID
                    }))
                    // this.chargeProjectTypes = res.data.content;
                }
            )
        },
         //获取计量单位
        getUint() {
            let data = {};
            apiBase.DoService(
            data,
            apiBase.getApiAdress().Code.GetUint,
            res => {
                let cdhTypeData = res.data.content,
                legendsCdhData = cdhTypeData.map(item => ({
                    label: item.CodeName,
                    value: item.CodeValue
                }));
                this.chaUintsTypes = legendsCdhData;
            },
            res => {
                this.$message.error(res.data.msg);
            }
            );
        },
        //取消
        cancel() {
            this.$refs.dialog.close();
        },
        //提交
        submit(){
            this.$store.state.isloading = true;
            switch(this.order){
                case ADD:
                    this.addCreateDpg();
                    break;
                case MODIFYPROJECT:
                    this.ModifyDpg();
                    break;
                default:
            }
        },
        // 数据重置
        dataRepeat() {
            Object.assign(this.$data.chaData, this.$options.data().chaData);
        },
        refreshData() {
            this.$emit("refreshData");
        },

    },
    components: {
        sweetModal,
        // dataTable,
        // warehousetypeMain
    },
    created() {
      let data = {};
      apiBase.DoService(data, apiBase.getApiAdress().Param.GetDepotisPrice, res => {
         this.isshowmoney = res.data.content;
      });
      apiBase.DoService(data, apiBase.getApiAdress().Param.GetIsDPGPushCHA, res => {
         this.IsDPGPushCHA = res.data.content;
      });
    },
}
</script>

